package com.example.x6.configurationmaintenance.Activity;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.widget.Toast;
import com.example.x6.configurationmaintenance.DataBase.TimeTick.DatabaseOpration;
import com.example.x6.configurationmaintenance.Dialog.MDatePickDialog;
import com.example.x6.configurationmaintenance.Interface.OnMDatePickDialogYesClickListener;
import com.example.x6.configurationmaintenance.Interface.TimeTickDatabase.OnDatabaseErgodicListener;
import com.example.x6.configurationmaintenance.MyPreference.MySwitchPreference;
import com.example.x6.configurationmaintenance.R;
import com.example.x6.configurationmaintenance.Service.RebootService;

public class PowerKitActivity extends PreferenceActivity {
    private final String TAG = getClass().getSimpleName();
    private Preference addRebootTask;
    private DatabaseOpration databaseOpration;
    private MDatePickDialog datePickDialog;
    private Handler handler;
    private PreferenceCategory task_list;

    /* access modifiers changed from: protected */
    @RequiresApi(api = 24)
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.power_kit);
        this.handler = new Handler();
        DatabaseOpration.initializeInstance(this);
        this.databaseOpration = DatabaseOpration.getInstance();
        this.datePickDialog = new MDatePickDialog(this);
        this.task_list = (PreferenceCategory) findPreference(getResources().getString(R.string.task_list));
        this.addRebootTask = getPreferenceScreen().findPreference(getResources().getString(R.string.add_reboot_task));
        initView();
        initEvent();
        Intent intent = new Intent(this, RebootService.class);
        intent.setAction("com.example.x6.configurationmaintenance.Service.RebootService");
        startService(intent);
    }

    private void initView() {
        this.databaseOpration.setOnDatabaseErgodicListener(new OnDatabaseErgodicListener() {
            /* class com.example.x6.configurationmaintenance.Activity.PowerKitActivity.AnonymousClass1 */

            @Override // com.example.x6.configurationmaintenance.Interface.TimeTickDatabase.OnDatabaseErgodicListener
            public void databaseErgodic(String uuid, int hour, int min, int second, boolean isOneShot) {
                PowerKitActivity.this.syncPreference(uuid, hour, min, second, isOneShot);
            }

            @Override // com.example.x6.configurationmaintenance.Interface.TimeTickDatabase.OnDatabaseErgodicListener
            public void databaseErgodicFinished(boolean isEmpty) {
                if (isEmpty) {
                    PowerKitActivity.this.Toast(PowerKitActivity.this.getResources().getString(R.string.no_restart_task));
                }
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void Toast(final String toast) {
        if (toast != null && !toast.isEmpty()) {
            runOnUiThread(new Runnable() {
                /* class com.example.x6.configurationmaintenance.Activity.PowerKitActivity.AnonymousClass2 */

                public void run() {
                    Toast.makeText(PowerKitActivity.this, toast, 0).show();
                }
            });
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void syncPreference(final String uuid, final int hour, final int min, int second, final boolean isOneShot) {
        runOnUiThread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Activity.PowerKitActivity.AnonymousClass3 */

            public void run() {
                Resources resources;
                int i;
                final MySwitchPreference mySwitchPreference = new MySwitchPreference(PowerKitActivity.this);
                mySwitchPreference.setTitle(String.valueOf(hour) + ":" + String.valueOf(min));
                if (isOneShot) {
                    resources = PowerKitActivity.this.getResources();
                    i = R.string.restart_once;
                } else {
                    resources = PowerKitActivity.this.getResources();
                    i = R.string.restart_repeat;
                }
                mySwitchPreference.setSummary(resources.getString(i));
                mySwitchPreference.setChecked(true);
                mySwitchPreference.setID(uuid);
                mySwitchPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                    /* class com.example.x6.configurationmaintenance.Activity.PowerKitActivity.AnonymousClass3.AnonymousClass1 */

                    public boolean onPreferenceChange(Preference preference, Object o) {
                        if (((Boolean) o).booleanValue()) {
                            return true;
                        }
                        PowerKitActivity.this.task_list.removePreference(mySwitchPreference);
                        PowerKitActivity.this.databaseOpration.delete(mySwitchPreference.getID());
                        return true;
                    }
                });
                PowerKitActivity.this.task_list.addPreference(mySwitchPreference);
            }
        });
    }

    private void initEvent() {
        this.datePickDialog.setOnMDatePickDialogYesClickListener(new OnMDatePickDialogYesClickListener() {
            /* class com.example.x6.configurationmaintenance.Activity.PowerKitActivity.AnonymousClass4 */

            @Override // com.example.x6.configurationmaintenance.Interface.OnMDatePickDialogYesClickListener
            public void YesClicked(int hour, int min, boolean isOneshot) {
                Resources resources;
                int i;
                String str = PowerKitActivity.this.TAG;
                Log.w(str, String.valueOf(hour) + ":" + String.valueOf(min) + " " + String.valueOf(isOneshot));
                String uuid = String.valueOf(System.currentTimeMillis());
                if (!PowerKitActivity.this.databaseOpration.insert(hour, min, 0, isOneshot, uuid)) {
                    PowerKitActivity.this.Toast(PowerKitActivity.this.getResources().getString(R.string.task_existed));
                    return;
                }
                final MySwitchPreference switchPreference = new MySwitchPreference(PowerKitActivity.this);
                switchPreference.setTitle(String.valueOf(hour) + ":" + String.valueOf(min));
                if (isOneshot) {
                    resources = PowerKitActivity.this.getResources();
                    i = R.string.restart_once;
                } else {
                    resources = PowerKitActivity.this.getResources();
                    i = R.string.restart_repeat;
                }
                switchPreference.setSummary(resources.getString(i));
                switchPreference.setChecked(true);
                switchPreference.setID(uuid);
                switchPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                    /* class com.example.x6.configurationmaintenance.Activity.PowerKitActivity.AnonymousClass4.AnonymousClass1 */

                    public boolean onPreferenceChange(Preference preference, Object o) {
                        if (((Boolean) o).booleanValue()) {
                            return true;
                        }
                        PowerKitActivity.this.task_list.removePreference(switchPreference);
                        PowerKitActivity.this.databaseOpration.delete(switchPreference.getID());
                        return true;
                    }
                });
                PowerKitActivity.this.task_list.addPreference(switchPreference);
            }
        });
        this.addRebootTask.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            /* class com.example.x6.configurationmaintenance.Activity.PowerKitActivity.AnonymousClass5 */

            @RequiresApi(api = 24)
            public boolean onPreferenceClick(Preference preference) {
                PowerKitActivity.this.datePickDialog.show();
                return true;
            }
        });
    }

    /* access modifiers changed from: protected */
    public void onResume() {
        super.onResume();
    }

    /* access modifiers changed from: protected */
    public void onPause() {
        super.onPause();
    }
}
